package com.apes.hr.cultivate.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 培训计划
 * @author Linwz
 * @create 2019-06-18 10:11
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CultivatePlan.CONST_TABLE_NAME)
public class CultivatePlan extends Model {
    public static final String CONST_TABLE_NAME = "hr_cultivate_plan";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "培训计划", prefix = "J")
    @Comment("培训计划编码")
    private String id;   //培训计划编码

    @Column(name = "name", length = 16)
    @Comment("课程名称")
    private String name;   //培训计划名称



    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cultivate_class", referencedColumnName = "id")
    @Comment("培训类别")
    private Enumeration cultivateClass; //培训类别

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "begin_date")
    @Comment("计划开始")
    private Date beginDate;  //计划开始

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "end_date")
    @Comment("计划结束")
    private Date endDate;  //计划结束

    @Column(name = "objective",length = 4000)
    @Comment("培训目的")
    private String objective;   //培训目的

    @Column(name = "note",length = 1200)
    @Comment("备注")
    private String note;//备注


    @Column(name = "state", length = 16)
    @Comment("状态")
    private String state;   //状态

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid; //审批人

    @Column(name = "approve_date")
    @Comment("审批日期")
    private Date approveDate; //审批日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_id", referencedColumnName = "id")
    private PartyRole deleteId; //删除人

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "delete_date")
    private Date deleteDate;    // 删除日期

    @OneToMany(mappedBy = "cultivatePlan", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<CultivatePlanItem> cultivatePlanItems = new HashSet(); //培训计划明细

    public Set<CultivatePlanItem> getCultivatePlanItems() {
        for (CultivatePlanItem cultivatePlanItem : cultivatePlanItems) {
            cultivatePlanItem.setCultivatePlan(this);
        }
        return cultivatePlanItems;
    }
}
